﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using HotelManagement.BLO;
using HotelManagement.DTO;

namespace HotelManagement.Services
{
    public partial class ServiceManagement : UserControl
    {
        public static CustomerDTO _customer;
        public static int _rentID;

        public static ServiceDTO _service;
        public static BillDetailDTO _billDetail;

        public ServiceManagement()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            _customer = new CustomerDTO();
            _service = new ServiceDTO();
            InitDataGridView();
            RefreshDataGridView();
        }
        private void InitDataGridView()
        {
            try
            {

                //Thêm cột xoa khách hàng
                DataGridViewButtonColumn use = new DataGridViewButtonColumn();
                use.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                use.Name = "UseColumn";
                use.HeaderText = "Chọn";
                use.Text = "Use";
                use.Visible = true;
                use.UseColumnTextForButtonValue = true;
                use.FlatStyle = FlatStyle.Popup;
                use.CellTemplate.Style.BackColor = Color.CadetBlue;
                dataGridViewService.Columns.Insert(0, use);
                //
                DataGridViewButtonColumn edit = new DataGridViewButtonColumn();
                edit.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                edit.Name = "EditColumn";
                edit.HeaderText = "Sửa";
                edit.Text = "Edit";
                edit.Visible = true;
                edit.UseColumnTextForButtonValue = true;
                edit.FlatStyle = FlatStyle.Popup;
                edit.CellTemplate.Style.BackColor = Color.CadetBlue;
                dataGridViewService.Columns.Insert(0, edit);

                dataGridViewService.Columns["ServiceID"].HeaderText = "Mã DV";
                dataGridViewService.Columns["ServiceName"].HeaderText = "Tên DV";
                dataGridViewService.Columns["Unit"].HeaderText = "Đơn vị";
                dataGridViewService.Columns["Price"].HeaderText = "Giá";
                
                for (int i = 0; i < dataGridViewService.Columns.Count; i++)
                {
                    if (i > 1)
                    {
                        dataGridViewService.Columns[i].ReadOnly = true;
                    }
                }
            }
            catch (System.Exception ea)
            {

            }
        }

        private void RefreshDataGridView()
        {
            BindingSource tblService = new BindingSource();
            //dataGridViewService.Rows.Clear();
            ServiceBLO bloS = new ServiceBLO();
            tblService.DataSource = bloS.Select();
            if (tblService==null)
            {
                return;
            }
            dataGridViewService.DataSource = tblService;
        }

        private void dataGridViewService_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            try
            {
                row = dataGridViewService.Rows[e.RowIndex];
                string colN = dataGridViewService.Columns[e.ColumnIndex].Name;
                if (colN == "EditColumn")
                {
                    ServiceDTO serv = new ServiceDTO();
                    serv.ServiceID = int.Parse(row.Cells["ServiceID"].Value.ToString());
                    serv.ServiceName = row.Cells["ServiceName"].Value.ToString();
                    serv.Unit = row.Cells["Unit"].Value.ToString();
                    serv.Price = float.Parse(row.Cells["Price"].Value.ToString());
                    ServiceEdit frm = new ServiceEdit(serv);
                    frm.ShowDialog();
                    if (frm.DialogResult==DialogResult.OK)
                    {
                        MessageBox.Show("Cập nhật thành công");
                        RefreshDataGridView();
                    }
                }else
                    if (colN=="UseColumn")
                    {
                        //Cập nhật lại biến _service toàn cục
                        _service.ServiceID = int.Parse(row.Cells["ServiceID"].Value.ToString());
                        _service.ServiceName = row.Cells["ServiceName"].Value.ToString();
                        _service.Unit = row.Cells["Unit"].Value.ToString();
                        _service.Price = float.Parse(row.Cells["Price"].Value.ToString());

                        //Đưa thông tin của _service vào các ô
                        txtServiceID.Text = _service.ServiceID.ToString();
                        txtServiceName.Text = _service.ServiceName;
                        txtServicePrice.Text = _service.Price.ToString();
                    }
            }
            catch (System.Exception ek)
            {

            }
        }

        private void btnCustomerSelect_Click(object sender, EventArgs e)
        {
            CustomerSelect frm = new CustomerSelect();
            frm.ShowDialog();
            if (frm.DialogResult==DialogResult.OK)
            {
                this.txtCustomerID.Text = _customer.CustomerID.ToString();
                txtCustomerName.Text = _customer.Name.ToString();
                txtCustomrIDNo.Text = _customer.IDNo_Passport.ToString();

            }
        }

        private void btnAddService_Click(object sender, EventArgs e)
        {
            ServiceEdit frm = new ServiceEdit();
            frm.ShowDialog();
            if (frm.DialogResult==DialogResult.OK)
            {
                MessageBox.Show("Thêm Dịch Vụ thành công");
                RefreshDataGridView();
                return;
            }
            MessageBox.Show("Thêm dịch vụ thất bại");
        }

        private void btnServiceStart_Click(object sender, EventArgs e)
        {
            BillDetailBLO bloBD = new BillDetailBLO();
            BillDetailDTO billD = new BillDetailDTO();
            BillBLO bloB = new BillBLO();
            int pBillID=bloB.GetBillIDFromARentID(_rentID);
            if (pBillID==0)
            {
                //MessageBox.Show("k")
                return;
            }
            billD.PaperID = pBillID;
            billD.ServiceID = int.Parse(txtServiceID.Text);
            billD.CustomerID = int.Parse(txtCustomerID.Text);
            billD.StartDate = DateTime.Now;

            if (bloBD.Insert(billD))
            {
                MessageBox.Show("Bắt đầu dùng dịch vụ");
                return;
            }
            MessageBox.Show("Khởi động dịch vụ thất bại");

        }

        private void btnSelectService_Click(object sender, EventArgs e)
        {
            StartedServicesForm frm = new StartedServicesForm();
            if (frm.ShowDialog()==DialogResult.Cancel)
            {
                return;
            }
            this.txtCustomerID.Text = _customer.CustomerID.ToString();
            txtCustomerName.Text = _customer.Name.ToString();
            txtCustomrIDNo.Text = _customer.IDNo_Passport;

            txtServiceID.Text = _service.ServiceID.ToString();
            txtServiceName.Text = _service.ServiceName;
            txtServicePrice.Text = _service.Price.ToString();
        }
    }
}
